import com.sun.java.browser.plugin2.DOM;

import java.util.Date;

//回顾继承
class Animal {
    public String name;
    public int age;

    public Animal(String name,int age) {
        this.name = name;
        this.age = age;
    }
    public void eat() {
        System.out.println(this.name+"正在吃饭！");
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Dog extends Animal {
    public Dog(String name,int age) {
        super(name,age);
    }
    public void wangWang() {
        System.out.println(this.name+"汪汪汪！");
    }
    @Override
    public void eat() {
        System.out.println(this.name+"吃狗粮！");
    }
}

class Bird extends Animal {
    public Bird(String name,int age) {
        super(name, age);
    }
    public void fly() {
        System.out.println(this.name+"正在飞！");
    }
}
public class Test8_1 {
    public static void main(String[] args) {
        Animal animal1 = new Bird("小鸟1",1);
        //animal1.fly();此时不能调用子类特有的fly()方法

        //向下转型 但是不安全
        Bird bird1 = (Bird) animal1;
        bird1.fly();

        Animal animal2 = new Dog("小狗2",2);
        if (animal2 instanceof Bird) {
            Bird bird2 = (Bird) animal2;
            bird2.fly();
        }else {
            System.out.println("不能飞！");
        }

    }
    public static void main3(String[] args) {
        //向上转型
        Animal animal = new Dog("小狗1",2);
        //通过父类引用 调用这个父类和子类重写的方法
        animal.eat();
//        animal.wangWang;//报错
    }
    public static void main2(String[] args) {
        //向上转型
//        Dog dog = new Dog("小狗",2);
//        Animal animal = dog;
        //父类引用  引用了子类对象
        Animal animal = new Dog("小狗",2);

        Dog dog1 = new Dog("小狗1",2);
        Bird bird1 = new Bird("小鸟1",1);
        func1(dog1);
        func1(bird1);

        Animal animal1 = func2();
        Animal animal2 = func3();
    }
    public static void func1(Animal animal) {

    }
    public static Animal func2() {
        return new Dog("小狗2",2);
    }
    public static Animal func3() {
        return new Bird("小鸟2",1);
    }
    public static void main1(String[] args) {
        Dog dog = new Dog("旺财",2);
        dog.eat();
        dog.wangWang();

        Animal animal = new Animal("animal",2);
        animal.eat();
        //animal.wangWang;//animal不能调用wangWang
        //只能访问Animal自己的方法
    }
}
